package com.rain.cy.business.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.base.Preconditions;
import com.rain.cy.business.model.dto.DoctorHomeDataDTO;
import com.rain.cy.business.model.dto.PatientHomeDataDTO;
import com.rain.cy.business.model.entity.UserEntity;
import com.rain.cy.business.service.*;
import com.rain.cy.framework.entity.BaseController;
import com.rain.cy.framework.result.Result;
import com.rain.cy.framework.result.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author richrain.cy
 * @date 2021/10/10 2:44 上午
 */
@RestController
@RequestMapping("/api/home")
public class HomeController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserScoreService userScoreService;
    @Autowired
    private CountService countService;

    @Autowired
    private CommonProcessService commonProcessService;

    @Autowired
    private UserProcessStatusService userProcessStatusService;

    @GetMapping("/doctor/data")
    @ApiOperation(value = "医生首页数据")
    public Result doctorHomeData() {
        Integer currentUserId = getCurrentUserId();
        List<UserEntity> users = userService.lambdaQuery().eq(UserEntity::getDoctorId, currentUserId).list();
        Map<Integer, String> processMap = commonProcessService.getProcessMap();
        if (CollectionUtil.isNotEmpty(users)) {
           users.forEach(userEntity -> userEntity.setCurrentProcessName(processMap.get(userProcessStatusService.getCurrentProcess(userEntity.getId()))));
        }
        DoctorHomeDataDTO homeDataDTO = DoctorHomeDataDTO.builder()
                .patientList(users)
                .userProcessCount(countService.getDoctorUserProcess(currentUserId))
                .build();
        return ResultUtil.success(homeDataDTO);
    }

    @GetMapping("/patient/data")
    @ApiOperation(value = "患者首页数据")
    public Result patientHomeData() {
        Integer currentUserId = getCurrentUserId();
        Preconditions.checkArgument(currentUserId != null, "患者ID不能为空");
        PatientHomeDataDTO homeDataDTO = userScoreService.getUserProcessScoreByPatientId(currentUserId);
        Map<Integer, String> processMap = commonProcessService.getProcessMap();
        homeDataDTO.currentProcess(userProcessStatusService.getCurrentProcess(currentUserId));
        homeDataDTO.setCurrentProcessTip(processMap.get(homeDataDTO.getCurrentProcess()));
        return ResultUtil.success(homeDataDTO);
    }

}
